import 'dart:math';

import 'package:flutter/material.dart';

class RecordPainter extends CustomPainter {
  List<double> fft;

  double barWidth = 10.0;
  double space = 1.0;
  double bottomSpace = 0.0;
  double topSpace = 0.0;

  RecordPainter(this.fft);

  @override
  void paint(Canvas canvas, Size size) {
    final leftPath = Path();
    barWidth = (size.width - 35 * 1) / 35;
    for (int i = 0; i < fft.length; i++) {
      final x = i * (barWidth + space) + space;
      final amplitude = fft[i];
      final y = translateAmplitudeToYPosition(amplitude, size);
      final barRect =
          Rect.fromLTWH(x, y, barWidth, size.height - bottomSpace - y + 50);
      leftPath.addRect(barRect);
    }
    final leftPaint = Paint()..color = Colors.blue;
    canvas.drawPath(leftPath, leftPaint);
  }

  double translateAmplitudeToYPosition(double amplitude, Size size) {
    final barHeight = amplitude * (size.height - bottomSpace - topSpace);
    return size.height - bottomSpace - barHeight;
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
